import {  exportExcelOne, getDutyCourseDetail2 } from "../../../../../utils/request/api";
import msg from "../../../../../utils/msg";
import { sr_data } from "../../util";
import User from "../../../../../utils/user";
// pages/user/s_worker/chooseTime/s_work/s_work.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showData:[]as sr_data[],//真正渲染的数据(防止一下太多数据渲染不过来)
    data:[] as sr_data[],//经过筛选后的数据
    data_origin:[] as sr_data[],//所有数据
    form:{
      type:'正常签到' as string,
      reason:'' as string,
    },
    sign_type:[],
    notSign:0,
    hasSign:0,
    all:0,
    other:0,
    input:'',
    options:[] as any,
  },
  cancel(){
    this.onClose();
  },
  exportOne:async function(){
    await msg.confirm('确定导出这一堂课的签到数据吗','');
    msg.Loading('导出数据中');
    let data = this.data.data;
    if(data.length==0){
      msg.show('人数不足1，无法导出','');return;
    }
    await exportExcelOne({data:{course_id:data[0].course_id,week:data[0].week,day:data[0].c_day,time:data[0].c_time},fileName:data[0].c_name});
    msg.hideLoading();
  },
  changeActive(e:any){
    let index = e.detail.index;
    if(index==0){
      this.search('');
    }
    else if(index==1){
      this.search('已签到');
    }
    else if(index==2){
      this.search('待签到');
    }
    else if(index==3){
      this.search('其他');
    }
  },
  getInput(e:any){
    this.setData({
      input:e.detail.value,
    });
    this.search(e.detail.value);
  },
  search(input:string){
    msg.Loading('加载中');
    let data = this.data.data_origin;
    let showData=[] as sr_data[];
    data.forEach((item)=>{
      if(item.type==input || item.s_name.includes(input) || item.s_code.includes(input)){
        showData.push(item);
      }
      else if(input=='已签到'&&(item.type=='正常签到'||item.type=='迟到'))showData.push(item);
      else if(input=='其他'&&(item.type!='正常签到'&&item.type!='迟到'&&item.type!='待签到'))showData.push(item);
    });
    if(input=="") {
      showData=data;
    }
    this._setdata(showData);
    msg.hideLoading();
  },
  
  onClose(){
    this.setData({
      detailShow:false,
    })
  },
  
  cal(){
    let data = this.data.data;
    let all=0;
    let hasSign=0;
    let notSign=0;
    let other=0;
    data.forEach((item)=>{
      all++;
      if(item.type=='待签到')notSign++;
      else if(item.type=='正常签到'||item.type=='迟到')hasSign++;
      else other++;
    });
    this.setData({
      all:all,
      hasSign:hasSign,
      notSign:notSign,
      other:other,
    });
  },
  
  edit(e:any){
    let index = e.currentTarget.dataset.index;
    //let 
    wx.navigateTo({
      url:"./detail/detail?data="+JSON.stringify(this.data.showData[index])+'&sign_type='+JSON.stringify(this.data.sign_type),
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options:{data:any,sign_type:any}) {
    msg.Loading('加载中');
    let user = new User;
    let data = JSON.parse(options.data);
    data['openid']=user.openid;
    this.setData({
      options:data,
    });
    getDutyCourseDetail2(data)
    .then((res:any)=>{
      this.setData({
        data:res.data.data,
        data_origin:JSON.parse(JSON.stringify(res.data.data)),
        sign_type:JSON.parse(options.sign_type),
      });
      this.cal();
      this._pushShowData();
      msg.hideLoading();
    })
    .catch(()=>{
      msg.hideLoading();
    })
    
  },
  onClickLeft(){
    wx.navigateBack();
  },
  //分层渲染
  //设置筛选结果
  _setdata(data:sr_data[]){
    let showData=[] as sr_data[];
    for(let i=0;(i<50&&i<data.length);i++)
      showData.push(data[i]);
    this.setData({
      data:data,
      showData:showData,
    });
  },
  //检测下拉渲染新数据
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom(){
    this._pushShowData();
  },
  //渲染新数据
  _pushShowData(){
    let data=this.data.data;
    let showData=this.data.showData;
    if(data.length<=showData.length)return;
    let start=showData.length+1;
    for(let i=start;(i<start+50&&i<data.length);i++)
      showData.push(data[i]);
    //console.log(showData.length);
    this.setData({
      showData:showData,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})